<!doctype html>
<html class="theme-next use-motion theme-next-mist">
<head>
  
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

  <meta name="google-site-verification" content="K8DCBviaoTBKVs28YBB7IBIbospQ9RVlgSh81RYMUhY" />


  <meta name="baidu-site-verification" content="tXr3ZTm3Hx" />



  <link rel="stylesheet" type="text/css" href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5"/>


<link rel="stylesheet" type="text/css" href="/css/main.css?v=0.4.5.1"/>

  <meta name="description" content="xgfe's blog. 鲜果前端的技术博客，鲜果前端研发部官方博客。前端基础技术研究：html, html5, javascript, css, css3；前端框架研究：angularJs, react, react native." />


  <meta name="keywords" content="Angular,指令,Directive," />


  <link rel="alternate" target="_blank" href="/atom.xml" title="xgfe" type="application/atom+xml" />


  <link rel="shorticon icon" type="image/x-icon" href="http://p0.meituan.net/xgfe/2db359f56ce13be30dedef160e0e57ce16958.ico?v=0.4.5.1" />

<meta name="description" content="下面通过一些简单的示例说明Angular指令中属性的值的获取方式以及函数的使用。">
<meta name="keywords" content="Angular,指令,Directive">
<meta property="og:type" content="article">
<meta property="og:title" content="Angular指令中的属性">
<meta property="og:url" content="http://xgfe.github.io/2016/08/12/y8n/angular-directive-attributes/index.html">
<meta property="og:site_name" content="xgfe">
<meta property="og:description" content="下面通过一些简单的示例说明Angular指令中属性的值的获取方式以及函数的使用。">
<meta property="og:updated_time" content="2017-07-06T09:59:23.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Angular指令中的属性">
<meta name="twitter:description" content="下面通过一些简单的示例说明Angular指令中属性的值的获取方式以及函数的使用。">


<script type="text/javascript" id="hexo.configuration">
  var CONFIG = {
    scheme: 'Mist',
    sidebar: 'post'
  };
</script>

  <title> Angular指令中的属性 | xgfe </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  <div style="position: fixed; top: -9999px; left: -9999px;">
    <img src="http://p0.meituan.net/xgfe/082a9624ba5ae8602150a2d43968463e49348.png" alt="xgfe"/>
  </div>
  <!--[if lte IE 8]>
  <div style=' clear: both; height: 59px; padding:0 0 0 15px; position: relative;margin:0 auto;'>
    <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
      <img src="http://7u2nvr.com1.z0.glb.clouddn.com/picouterie.jpg" border="0" height="42" width="820"
           alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today or use other browser ,like chrome firefox safari."
           style='margin-left:auto;margin-right:auto;display: block;'/>
    </a>
  </div>
<![endif]-->
  

  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?3601d4483819a5ab6ddabb0b6422a328";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <div class="container one-column page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><h1 class="site-meta">
  <span class="logo-line-before"><i></i></span>
  <a href="/" class="brand" rel="start">
      <span class="logo">
        <i class="icon-next-logo"></i>
      </span>
      <span class="site-title">xgfe</span>
  </a>
  <span class="logo-line-after"><i></i></span>
</h1>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  
  

  
    <ul id="menu" class="menu menu-left">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            <i class="menu-item-icon icon-next-home"></i> <br />
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            <i class="menu-item-icon icon-next-archives"></i> <br />
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            <i class="menu-item-icon icon-next-tags"></i> <br />
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-join">
          <a href="/join" rel="section">
            <i class="menu-item-icon icon-next-join"></i> <br />
            加入我们
          </a>
        </li>
      
      <!-- slide-links added by felix -->
      <li class="menu-item menu-item-slides" style="opacity: 1; transform: translateY(0px);">
        <a href="http://xgfe.github.io/Basics/" target="_blank" rel="section">
          <i class="menu-item-icon icon-next-slides"></i> <br>
          Basics
        </a>
      </li>
      <li class="menu-item menu-item-slides" style="opacity: 1; transform: translateY(0px);">
        <a href="https://slides.com/xgfe" target="_blank" rel="section">
          <i class="menu-item-icon icon-next-slides"></i> <br>
          Slides
        </a>
      </li>

      
      
    </ul>
  

  
    <div class="site-search">
      

    </div>
  

    <div class="site-search">
      <form class="site-search-form" id="gg-form" action="https://www.google.com/webhp" >
        <input type="text" name="q" id="gg-search-input" class="menu-search-input">
      </form>
    </div>
</nav>
 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div id="content" class="content"> 

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">

      
      
        <h1 class="post-title" itemprop="name headline">
          
          
            
              Angular指令中的属性
            
          
        </h1>
      

      <div class="post-meta">
        <span class="post-time">
          发表于
          <time itemprop="dateCreated" datetime="2016-08-12T17:18:08+08:00" content="2016-08-12">
            2016-08-12
          </time>
        </span>

        
          <span class="post-category" >
            &nbsp; | &nbsp; 作者
            
              <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                <a href="/categories/y8n/" itemprop="url" rel="index">
                  <span itemprop="name">y8n</span>
                </a>
              </span>

              
              

            
          </span>
        

        
          
        

        <!-- tags 挪动位置 -->
        
          <span class="post-tags">
            &nbsp; | &nbsp;
            
              <a href="/tags/Angular/" rel="tag"><i class="icon-next-tags"></i>Angular</a>
            
              <a href="/tags/指令/" rel="tag"><i class="icon-next-tags"></i>指令</a>
            
              <a href="/tags/Directive/" rel="tag"><i class="icon-next-tags"></i>Directive</a>
            
          </span>
        
      </div>
    </header>

    <div class="post-body">

      
      

      
        <span itemprop="articleBody"><p>下面通过一些简单的示例说明Angular指令中属性的值的获取方式以及函数的使用。</p>
<a id="more"></a>
<h2 id="直接获取"><a href="#直接获取" class="headerlink" title="直接获取"></a>直接获取</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">app.directive(&apos;my-directive&apos;,function()&#123;</div><div class="line">	return &#123;</div><div class="line">		scope:&#123;</div><div class="line">			attr1:&apos;@&apos;,</div><div class="line">			attr2:&apos;=&apos;,</div><div class="line">			attr3:&apos;&amp;&apos;</div><div class="line">		&#125;,</div><div class="line">		link: function(scope)&#123;</div><div class="line">			// scope.attr1</div><div class="line">			// scope.attr2</div><div class="line">			// scope.attr3</div><div class="line">		&#125;</div><div class="line">	&#125;;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">&lt;my-directive attr1=&quot;&#123;&#123;value&#125;&#125;&quot; attr2=&quot;value2&quot; attr3=&quot;value3()&quot;&gt;&lt;/my-directive&gt;</div></pre></td></tr></table></figure>
<ul>
<li><code>attr1</code> 是一次性获取</li>
<li><code>attr2</code> 是双向绑定</li>
<li><code>attr3</code> 是方法，可以在指令内部调用</li>
</ul>
<h2 id="手动解析传递的变量"><a href="#手动解析传递的变量" class="headerlink" title="手动解析传递的变量"></a>手动解析传递的变量</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">app.directive(&apos;my-directive&apos;,function()&#123;</div><div class="line">	return &#123;</div><div class="line">		scope:&#123;&#125;,</div><div class="line">		link: function(scope,ele,attrs)&#123;</div><div class="line">			var attr1 = attrs.attr1;    // &apos;Hello&apos;</div><div class="line">			var attr2 = scope.$parent.$eval(attrs.attr2); // &apos;Hello&apos;</div><div class="line">		&#125;</div><div class="line">	&#125;;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">&lt;my-directive attr1=&quot;&#123;&#123;value&#125;&#125;&quot; attr2=&quot;value&quot;&gt;&lt;/my-directive&gt;</div><div class="line"></div><div class="line">app.controller(&apos;myController&apos;,function($scope)&#123;</div><div class="line">	$scope.value = &apos;Hello&apos;;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>注意：上述这种做法用到了指令的<strong>父级作用域</strong>，所以指令中设置<code>scope:false</code>是不行的，尽量使用独立的作用域。</p>
<p>这样也可以实现数据的watch</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	var attr1 = attrs.attr1;    // &apos;Hello&apos;</div><div class="line">	attrs.$observe(&apos;attr1&apos;,function()&#123;</div><div class="line">		console.log(&apos;attr1 changed&apos;);</div><div class="line">	&#125;);</div><div class="line">	var attr2 = scope.$parent.$eval(attrs.attr2); // &apos;Hello&apos;</div><div class="line">	scope.$parent.$watch(attrs.attr2,function()&#123;</div><div class="line">		console.log(&apos;attr2 chaned&apos;);</div><div class="line">	&#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="使用-parse"><a href="#使用-parse" class="headerlink" title="使用$parse"></a>使用$parse</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	var attr2 = $parse(attrs.attr2)(scope.$parent); // &apos;Hello&apos;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>其实原理和<code>$eval</code>差不多，使用<code>$eval</code>其实也是间接调用了<code>$parse</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Angular中$eval的代码</div><div class="line">$eval: function(expr, locals) &#123;</div><div class="line">	return $parse(expr)(this, locals);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>同样，使用<code>$parse</code>也可以实现<code>watch</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	scope.$parent.$watch($parse(attrs.attr2),function()&#123;</div><div class="line">		console.log(&apos;attr2 chaned&apos;);</div><div class="line">	&#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="方法的传递和使用"><a href="#方法的传递和使用" class="headerlink" title="方法的传递和使用"></a>方法的传递和使用</h2><p>使用以上说的<code>$eval</code>或者<code>$parse</code>方法解析函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">外层的controller</div><div class="line">app.controller(&apos;myController&apos;,function($scope)&#123;</div><div class="line">	$scope.onChange = function(argv)&#123;</div><div class="line">		return &apos;Hello &apos;+argv;</div><div class="line">	&#125;;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[例子一]</div><div class="line">直接传递方法名，就类似于传递变量，可以在指令中调用该方法</div><div class="line">&lt;my-directive on-change=&quot;onChange&quot;&gt;&lt;/my-directive&gt;</div><div class="line"></div><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	var onChange = scope.$parent.$eval(attrs.onChange);</div><div class="line">	console.log(onChange(&apos;World&apos;)); // Hello World</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[例子二]</div><div class="line">传递方法的调用</div><div class="line">&lt;my-directive on-change=&quot;onChange(&apos;Angular&apos;)&quot;&gt;&lt;/my-directive&gt;</div><div class="line"></div><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	var onChange = scope.$parent.$eval(attrs.onChange);</div><div class="line">	console.log(onChange); // Hello Angular</div><div class="line">	console.log(onChange(&apos;World&apos;)); // Error</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>使用<code>&amp;</code>的写法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">指令</div><div class="line">app.directive(&apos;my-directive&apos;,function()&#123;</div><div class="line">	return &#123;</div><div class="line">		scope:&#123;</div><div class="line">			onChange:&apos;&amp;&apos;</div><div class="line">		&#125;,</div><div class="line">		link: function(scope,ele,attrs)&#123;</div><div class="line">			// ...</div><div class="line">		&#125;</div><div class="line">	&#125;;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[例子三]</div><div class="line">&lt;my-directive on-change=&quot;onChange&quot;&gt;&lt;/my-directive&gt;</div><div class="line"></div><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	var onChange = scope.onChange;</div><div class="line">	console.log(onChange); // 经过Angular封装的函数，并不是父级作用域中的onChange函数</div><div class="line">	console.log(onChange()); // 第一次调用，获取到onChange函数</div><div class="line">	console.log(onChange()(&apos;Directive&apos;)); // 最终的执行结果 Hello Directive</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[例子四]</div><div class="line">&lt;my-directive on-change=&quot;onChange(&apos;Hello&apos;)&quot;&gt;&lt;/my-directive&gt;</div><div class="line"></div><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	var onChange = scope.onChange;</div><div class="line">	console.log(onChange); // 经过Angular封装的函数，并不是父级作用域中的onChange函数</div><div class="line">	console.log(onChange(&apos;invoke&apos;)); // 最终的执行结果，输出 &apos;Hello Hello&apos; 而不是 &apos;Hello invoke&apos;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>可以发现，如果使用<code>&amp;</code>在<code>scope</code>中指定一个属性为函数的话，直接从<strong><code>scope</code></strong>上获取的是经过<code>Angular</code>封装的一个函数,经过调用这个函数返回的值才是指令属性上真正绑定的值。如果你在指令上只是要绑定一个直接执行的函数，类似于[例子四]是比较常用的做法。</p>
<h2 id="指令中使用函数时值的传递"><a href="#指令中使用函数时值的传递" class="headerlink" title="指令中使用函数时值的传递"></a>指令中使用函数时值的传递</h2><p>上面介绍了如何使用函数，这里简要介绍一下函数中如何传递值，即指令内部的值如何通过函数传递出来。</p>
<p>如上面的[例子三]，可以将数据传递出来，这是一种方法，不过在用法上不是很好，因为这样的话在指令中绑定的并不是<strong>函数</strong>，而是<strong>变量</strong>，下面介绍一个更好的方法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;button type=&quot;button&quot; on-click=&quot;onClickHandler($event)&quot;&gt;按钮&lt;/button&gt;</div></pre></td></tr></table></figure>
<p>上面的代码使我们经常会使用的，在<code>angular</code>自带的<code>onClick</code>函数中可以使用<code>$event</code>参数来获取点击事件的相应内容，从而在使用指令的作用域中进行操作，那么如何才能把指令内部的值传递到外面呢？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">外层的controller</div><div class="line">app.controller(&apos;myController&apos;,function($scope,$interval)&#123;</div><div class="line">	$scope.onChange = function(count,timer)&#123;</div><div class="line">		if(count &gt; 9)&#123;</div><div class="line">			$interval.cancel(timer);</div><div class="line">		&#125;</div><div class="line">		console.log(&apos;Hello &apos;+ count);</div><div class="line">	&#125;;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">&lt;my-directive on-change=&quot;onChange($argv,$timer)&quot;&gt;&lt;/my-directive&gt;</div><div class="line"></div><div class="line">link: function(scope,ele,attrs)&#123;</div><div class="line">	var onChange = scope.onChange;</div><div class="line">	var count = 0;</div><div class="line">	var timer = $interval(function()&#123;</div><div class="line">		onChange(&#123;</div><div class="line">			$argv: count++,</div><div class="line">			$timer: timer</div><div class="line">		&#125;);</div><div class="line">	&#125;,1000);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>上述代码会指令内的值通过函数传递到外部作用域。这种使用场景并不是很多，不过非常有用，比如<code>ng-file-upload</code>中的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div class=&quot;button&quot; ngf-select=&quot;upload($file)&quot;&gt;Upload on file select&lt;/div&gt;</div></pre></td></tr></table></figure>
<p>就是通过这种方式传递的<code>$file</code>对象。</p>
</span>
      
    </div>

    <footer class="post-footer">

      
        <div class="post-nav">
          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/08/21/lulutia/react-native-touch-animation/" rel="prev">React-Native 触摸与动画</a>
            
          </div>

          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/08/11/penglu/effective-javascript/" rel="next">编写高质量JS代码</a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>

 </div>

        

        
          <div class="comments" id="comments">
            <div id="SOHUCS" sid="" ></div>
          </div>
        
      </div>

      
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <a href="https://github.com/xgfe" target="_blank"><img class="site-author-image" src="http://p0.meituan.net/xgfe/082a9624ba5ae8602150a2d43968463e49348.png" alt="xgfe" itemprop="image"/></a>
          <p class="site-author-name" itemprop="name">xgfe</p>
        </div>
        <p class="site-description motion-element" itemprop="description">xgfe's blog. 鲜果前端的技术博客，鲜果前端研发部官方博客。前端基础技术研究：html, html5, javascript, css, css3；前端框架研究：angularJs, react, react native.</p>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">89</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          <div class="site-state-item site-state-categories">
            
              <span class="site-state-item-count">37</span>
              <span class="site-state-item-name">作者</span>
              
          </div>

          <div class="site-state-item site-state-tags">
            <a href="/tags">
              <span class="site-state-item-count">131</span>
              <span class="site-state-item-name">标签</span>
              </a>
          </div>

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" target="_blank" rel="alternate">
              <i class="menu-item-icon icon-next-feed"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/xgfe" target="_blank">GitHub</a>
              </span>
            
          
        </div>

        
        

        <div class="links-of-author motion-element">
          
        </div>

      </section>

      
        <section class="post-toc-wrap sidebar-panel-active">
          <div class="post-toc-indicator-top post-toc-indicator"></div>
          <div class="post-toc">
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#直接获取"><span class="nav-number">1.</span> <span class="nav-text">直接获取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#手动解析传递的变量"><span class="nav-number">2.</span> <span class="nav-text">手动解析传递的变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用-parse"><span class="nav-number">3.</span> <span class="nav-text">使用$parse</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#方法的传递和使用"><span class="nav-number">4.</span> <span class="nav-text">方法的传递和使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#指令中使用函数时值的传递"><span class="nav-number">5.</span> <span class="nav-text">指令中使用函数时值的传递</span></a></li></ol></div>
            
          </div>
          <div class="post-toc-indicator-bottom post-toc-indicator"></div>
        </section>
      

    </div>
  </aside>


    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner"> <div class="copyright" >
  
  &copy; &nbsp; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="icon-next-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xgfe</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" target="_blank" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


 </div>
    </footer>

    <div class="back-to-top"></div>
  </div>

  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  
  
    <script type="text/javascript"> 
(function(){ 
var appid = 'cysWiXvkm'; 
var conf = 'prod_fc970dbe85103c7a79b2c4f3dc7fb190'; 
var width = window.innerWidth || document.documentElement.clientWidth; 
if (width < 960) { 
window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="http://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("http://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})}); } })(); </script>
    

  


  
  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js"></script>
  <script type="text/javascript" src="/js/fancy-box.js?v=0.4.5.1"></script>


  <script type="text/javascript" src="/js/helpers.js?v=0.4.5.1"></script>
  

  <script type="text/javascript" src="/vendors/velocity/velocity.min.js"></script>
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js"></script>

  <script type="text/javascript" src="/js/motion_global.js?v=0.4.5.1" id="motion.global"></script>




  <script type="text/javascript" src="/js/nav-toggle.js?v=0.4.5.1"></script>
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  
<script type="text/javascript" src="/js/bootstrap.scrollspy.js?v=0.4.5.1" id="bootstrap.scrollspy.custom"></script>


<script type="text/javascript" id="sidebar.toc.highlight">
  $(document).ready(function () {
    var tocSelector = '.post-toc';
    var $tocSelector = $(tocSelector);
    var activeCurrentSelector = '.active-current';

    $tocSelector
      .on('activate.bs.scrollspy', function () {
        var $currentActiveElement = $(tocSelector + ' .active').last();

        removeCurrentActiveClass();
        $currentActiveElement.addClass('active-current');

        $tocSelector[0].scrollTop = $currentActiveElement.position().top;
      })
      .on('clear.bs.scrollspy', function () {
        removeCurrentActiveClass();
      });

    function removeCurrentActiveClass () {
      $(tocSelector + ' ' + activeCurrentSelector)
        .removeClass(activeCurrentSelector.substring(1));
    }

    function processTOC () {
      getTOCMaxHeight();
      toggleTOCOverflowIndicators();
    }

    function getTOCMaxHeight () {
      var height = $('.sidebar').height() -
                   $tocSelector.position().top -
                   $('.post-toc-indicator-bottom').height();

      $tocSelector.css('height', height);

      return height;
    }

    function toggleTOCOverflowIndicators () {
      tocOverflowIndicator(
        '.post-toc-indicator-top',
        $tocSelector.scrollTop() > 0 ? 'show' : 'hide'
      );

      tocOverflowIndicator(
        '.post-toc-indicator-bottom',
        $tocSelector.scrollTop() >= $tocSelector.find('ol').height() - $tocSelector.height() ? 'hide' : 'show'
      )
    }

    $(document).on('sidebar.motion.complete', function () {
      processTOC();
    });

    $('body').scrollspy({ target: tocSelector });
    $(window).on('resize', function () {
      if ( $('.sidebar').hasClass('sidebar-active') ) {
        processTOC();
      }
    });

    onScroll($tocSelector);

    function onScroll (element) {
      element.on('mousewheel DOMMouseScroll', function (event) {
          var oe = event.originalEvent;
          var delta = oe.wheelDelta || -oe.detail;

          this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
          event.preventDefault();

          toggleTOCOverflowIndicators();
      });
    }

    function tocOverflowIndicator (indicator, action) {
      var $indicator = $(indicator);
      var opacity = action === 'show' ? 0.4 : 0;
      $indicator.velocity ?
        $indicator.velocity('stop').velocity({
          opacity: opacity
        }, { duration: 100 }) :
        $indicator.stop().animate({
          opacity: opacity
        }, 100);
    }

  });
</script>

<script type="text/javascript" id="sidebar.nav">
  $(document).ready(function () {
    var html = $('html');
    var TAB_ANIMATE_DURATION = 200;
    var hasVelocity = $.isFunction(html.velocity);

    $('.sidebar-nav li').on('click', function () {
      var item = $(this);
      var activeTabClassName = 'sidebar-nav-active';
      var activePanelClassName = 'sidebar-panel-active';
      if (item.hasClass(activeTabClassName)) {
        return;
      }

      var currentTarget = $('.' + activePanelClassName);
      var target = $('.' + item.data('target'));

      hasVelocity ?
        currentTarget.velocity('transition.slideUpOut', TAB_ANIMATE_DURATION, function () {
          target
            .velocity('stop')
            .velocity('transition.slideDownIn', TAB_ANIMATE_DURATION)
            .addClass(activePanelClassName);
        }) :
        currentTarget.animate({ opacity: 0 }, TAB_ANIMATE_DURATION, function () {
          currentTarget.hide();
          target
            .stop()
            .css({'opacity': 0, 'display': 'block'})
            .animate({ opacity: 1 }, TAB_ANIMATE_DURATION, function () {
              currentTarget.removeClass(activePanelClassName);
              target.addClass(activePanelClassName);
            });
        });

      item.siblings().removeClass(activeTabClassName);
      item.addClass(activeTabClassName);
    });

    $('.post-toc a').on('click', function (e) {
      e.preventDefault();
      var targetSelector = escapeSelector(this.getAttribute('href'));
      var offset = $(targetSelector).offset().top;
      hasVelocity ?
        html.velocity('stop').velocity('scroll', {
          offset: offset  + 'px',
          mobileHA: false
        }) :
        $('html, body').stop().animate({
          scrollTop: offset
        }, 500);
    });

    // Expand sidebar on post detail page by default, when post has a toc.
    var $tocContent = $('.post-toc-content');
    if (isDesktop() && CONFIG.sidebar === 'post') {
      if ($tocContent.length > 0 && $tocContent.html().trim().length > 0) {
        displaySidebar();
      }
    }
  });
</script>



  <script type="text/javascript">
    $(document).ready(function () {
      if (CONFIG.sidebar === 'always') {
        displaySidebar();
      }
      if (isMobile()) {
        FastClick.attach(document.body);
      }
    });
  </script>

  

  
  

  
  <script type="text/javascript" src="/js/lazyload.js"></script>
  <script type="text/javascript">
    $(function () {
      $("#posts").find('img').lazyload({
        placeholder: "/images/loading.gif",
        effect: "fadeIn"
      });
    });
  </script>

  <!-- google search, added by felix -->
  <script>
      $('#gg-form').on('submit', function(e) {
        var keyword = $.trim($(this).find('#gg-search-input').val());
        if (keyword) {
          location.href = 'https://www.google.com.hk/?gfe_rd=cr&ei=hXw8VpjtHuLC8AeSuIjQAg&gws_rd=ssl#safe=strict&q='+encodeURIComponent(keyword)+'+site:xgfe.github.io';
        }
        return false;
      });
  </script>
  <!-- baidu 站长自动推送 -->
  <script>
  (function(){
      var bp = document.createElement('script');
      bp.src = '//push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>
</body>
</html>
